package com.dayuanit.dy17.mix.thread;

import java.util.concurrent.*;

public class ThreadPoolBizFuture {
    private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5,
            5,
            60,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(5));

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        pay();

        Future sendEmailFuture = threadPool.submit(new Runnable() {
            @Override
            public void run() {
                sendEmail();
            }
        });

        Future sendMessageFuture = threadPool.submit(new Runnable() {
            @Override
            public void run() {
                sendMessage();
            }
        });

        Future riskmangerFuture = threadPool.submit(new Runnable() {
            @Override
            public void run() {
                riskmanger();
            }
        });

        Future dataDBFuture = threadPool.submit(new Runnable() {
            @Override
            public void run() {
                dataBase();
            }
        });

        //get方法会等待任务的结束，任务结束，get方法才会返回，当前线程继续向下执行
        Object result = sendEmailFuture.get();
        System.out.println("result=" + result);//null
        sendMessageFuture.get();
        riskmangerFuture.get();
        dataDBFuture.get();

        System.out.println("支付成功了");
    }

    public static void sleep() {
        try {
            Thread.sleep(1 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void pay() {
        System.out.println("pay()");
        sleep();
    }

    public static void sendEmail() {
        System.out.println("sendEmail()");
        sleep();
    }

    public static void sendMessage() {
        System.out.println("sendMessage()");
        sleep();
    }

    public static void riskmanger() {
        System.out.println("riskmanger()");
        sleep();
    }

    public static void dataBase() {
        System.out.println("dataBase()");
        sleep();
    }
}
